#include "add_admin.h"
#include "ui_add_admin.h"
#include <string.h>
#include <string>
#include <cstring>
#include <QString>
#include <QMessageBox>
#include "student.h"
#include "studentstore.h"
#include "utils.h"
using namespace student;
using namespace studentstore;
using namespace std;
using namespace utils;
add_admin::add_admin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::add_admin) {
    ui->setupUi(this);
}

add_admin::~add_admin() {
    delete ui;
}

void add_admin::on_pushButton_clicked() {
    QString str = ui->lineEdit->text();
    string stu_id = string((const char *)str.toLocal8Bit());
    Student* stu = search_oneReturn<Student>("stu_id", stu_id, studentDatPath) ;
    Student* temp = new Student ();
    memset(temp, 0, sizeof (Student));
    if(stu_id[0] != '#')
        QMessageBox::warning(this, "warning", "格式错误");
    else if(stu_id.size() >= 16 || stu_id.size() < 2) {
        QMessageBox::warning(this, "warning", "账号需要在2到16字节之间");
    } else if(memcmp(stu, temp, sizeof (Student)) == 0) {
        string name = stu_id;
        string pwd = "123456";
        string major = " ";

        //hash密码
        string salt;        //生成盐字符串
        salt = rand_str(5);
        string pwdHash = sha1(sha1(pwd) + salt);

        stu = creatStudent(name, stu_id, pwdHash, salt, major);
        stu->type = 0; //设置为管理员type
        addStudent(stu);
        QMessageBox::information(this, "info", "添加管理员成功");
        ui->lineEdit->clear();
    } else {
        QMessageBox::warning(this, "warning", "管理员已存在");
    }
    delete stu;
}
